package com.aiju.support.mvp;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.aiju.common.PixValue;

/**
 * @author: 佐为
 * @date: 2017-02-28 19:28
 * @version: V1.0
 * @Description: 描述
 */
public class MyTextView extends View {
    /**
     * 需要绘制的文字
     */
    private String mLeftText, mRightText;
    /**
     * 文本的颜色
     */
    private int mLeftTextColor, mRightTextColor;
    /**
     * 文本的大小
     */
    private int mLeftTextSize, mRightTextSize;
    /**
     * 绘制时控制文本绘制的范围
     */
    private Rect mLeftBound, mRightBound;

    private Paint mLeftPaint, mRightPaint;

    private boolean leftBold, rightBold;

    private int padding = PixValue.dp.valueOf(5);

    public MyTextView(Context context) {
        this(context, null);
    }

    public MyTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //初始化
        mLeftText = "8.8";
        mLeftTextColor = Color.BLACK;
        leftBold = true;
        mLeftTextSize = 100;

        mRightTextColor = Color.RED;
        mRightText = "折";
        mRightTextSize = 50;


        mLeftPaint = new Paint();
        if (leftBold){
            mLeftPaint.setAntiAlias(true);
        }
        mLeftPaint.setTextSize(mLeftTextSize);
        mLeftPaint.setColor(mLeftTextColor);
        mLeftBound = new Rect();
        mLeftPaint.getTextBounds(mLeftText, 0, mLeftText.length(), mLeftBound);

        mRightPaint = new Paint();
        if (rightBold){
            mLeftPaint.setAntiAlias(true);
        }
        mRightPaint.setTextSize(mRightTextSize);
        mRightPaint.setColor(mRightTextColor);
        mRightBound = new Rect();
        mRightPaint.getTextBounds(mLeftText, 0, mLeftText.length(), mRightBound);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        //绘制文字
        canvas.drawText(mLeftText, getWidth() / 2 - mLeftBound.width() / 2, getHeight() / 2 + mLeftBound.height() / 2, mLeftPaint);

        canvas.drawText(mRightText, getWidth() / 2 + mLeftBound.width() / 2 + padding, getHeight() / 2 + mLeftBound.height() / 2, mRightPaint);
    }
}
